package uk.ac.lkl.common.allocator.ui;



//import java.awt.Point;
//import java.awt.event.MouseEvent;

//import javax.swing.event.MouseInputAdapter;

//import uk.ac.lkl.common.allocator.AllocatorModel;
//import uk.ac.lkl.common.allocator.ScheduleObject;



public class CreateDragger<O> extends Dragger<O> {

    private int dragStart;

    private int lastDragEnd;


    public CreateDragger(SwitchingAllocatorView<O> scheduleView) {
        super(scheduleView);
    }


    public void processMousePressed(O object, int row, int column) {
        dragStart = column;
        lastDragEnd = column;
    }


    public void processMouseDragged(O object, int row, int column) {
      int lastStart = Math.min(dragStart, lastDragEnd);
      int lastEnd = Math.max(dragStart, lastDragEnd);

      //if (lastDragEnd == column)
      //    return;
      
      for (int i = lastStart; i<= lastEnd; i++)
          scheduleView.setMobileCell(object, i, false);

      schedule.unallocate(object, lastStart, lastEnd);
      
      boolean positiveDirection = column >= dragStart;
      int step = positiveDirection ? 1 : -1;

      boolean allOk = true;
      
      for (int i = dragStart; i != column + step; i += step) {
          boolean ok = schedule.pushSetAllocation(object, i, 1, positiveDirection);
          if (ok)
              scheduleView.setMobileCell(object, i, true);
          else {
              allOk = false;
              break;
          }
      }
      
      if (allOk)
              lastDragEnd = column;
        
    }


    public void processMouseReleased(O object, int row, int column) {

    }

}
